package com.ruoyi.web.pojo;

import java.io.Serializable;

import lombok.Getter;
import lombok.Setter;

import static com.ruoyi.constant.HttpStatus.SUCCESS;
import static com.ruoyi.constant.HttpStatus.ERROR;

/**
 * 响应信息主体
 */
@Getter
@Setter
public class R<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return createResult(null, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data) {
        return createResult(data, SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data, String msg) {
        return createResult(data, SUCCESS, msg);
    }


    public static <T> R<T> fail() {
        return createResult(null, ERROR, "操作失败");
    }

    public static <T> R<T> fail(String msg) {
        return createResult(null, ERROR, msg);
    }

    public static <T> R<T> fail(T data) {
        return createResult(data, ERROR, "操作失败");
    }

    public static <T> R<T> fail(T data, String msg) {
        return createResult(data, ERROR, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return createResult(null, code, msg);
    }


    private static <T> R<T> createResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
